package design.pattern.singleton;


import java.util.ArrayList;
import java.util.List;

/**
 * 懒汉式-双检锁模式
 */
public class LazySingleton {


	private LazySingleton(){
		System.out.println("LazySingleton is create");
	}

	private static LazySingleton instance = null;

	public static void main(String[] args) throws InterruptedException {

		for (int i = 0;i<100;i++) {
			Thread thread = new Thread(() -> {
				LazySingleton.getInstance();
			});
			thread.start();
		}

	}


	public static   LazySingleton getInstance() {


		if (instance!=null){
			System.out.println("instance 不为空直接返回");
			return instance;
		}
		synchronized (Object.class) {
			if (instance!=null){
				System.out.println("instance 不为空直接返回");
				return instance;
			}
			System.out.println("instance 为空创建实例");
			instance = new LazySingleton();
			return instance;
		}
	}
}
